//
//  TaTaMiViewController.m
//  GKBHouse
//
//  Created by 林矗 on 17/2/8.
//
//

#import "TaTaMiViewController.h"

@interface TaTaMiViewController ()
@property (retain, nonatomic) IBOutlet UIButton *up;
@property (retain, nonatomic) IBOutlet UIButton *down;
@property (retain, nonatomic) IBOutlet UIButton *stop;
@property (nonatomic ,strong) NewEquipment *equipment;
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, copy) NSString *ctrOrder;
@end

@implementation TaTaMiViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (id)initWithEquipment:(NewEquipment *)equ
{
    self = [super init];
    if (self) {
        // Custom initialization
        self.equipment = equ;
    }
    return self;
}
- (void)closeView
{
    [GKBMainController refreshDevices];
    [self.view removeFromSuperview];
    [GKBMainController postStartRefresh];
}
- (void)initTopBar
{
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(closeView) withTitle:self.equipment.name];
    [self.view addSubview:topBar];
    [topBar release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _imageView.layer.borderColor = [UIColor blackColor].CGColor;
    
    _imageView.layer.borderWidth = 10;
    
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [self.view setFrame:CGRectMake(0, 0, DEVICE_HEIGHT, DEVICE_WIDTH)];
    [self initTopBar];
    
    [_up setTitle:NSLocalizedString(@"TTM-Up", nil) forState:UIControlStateNormal];
    [_down setTitle:NSLocalizedString(@"TTM-Down", nil) forState:UIControlStateNormal];
    [_stop setTitle:NSLocalizedString(@"TTM-Stop", nil) forState:UIControlStateNormal];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 电器设备开关状态
- (void)ctlDevice:(BOOL)open
{
    NSString *switchId = self.equipment.switchId;
    NSString *name = self.equipment.name;
    BOOL STATE = [DeviceView deviceStateOn:self.equipment];
    BOOL OPEN = !STATE;
    [DeviceView OperatSwithcId:switchId name:name OnOff:open target:self end:@selector(ctlDeviceEnd:)];
}
- (void)ctlDeviceEnd:(NSDictionary *)dic
{
    NSLog(@"%@", dic);
    
    NSNumber *result = [dic objectForKey:@"result"];
    
    NSString *resultTip = @"";
    if (result.integerValue == 0) {
        BOOL STATE = [DeviceView deviceStateOn:self.equipment ];
        BOOL OPEN = !STATE;
        //操作成功
        resultTip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        
        if(OPEN)
        {
            _equipment.state = kStateON;
        }else
        {
            _equipment.state = kStateOFF;
            
        }
        GKBMainController *main = [GKBMainController mainCtr];
        [main updateStateByNetTarget:self action:@selector(updateState:)];
    }
    else
    {
        //操作失败！原因：
        resultTip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
    }
    [iToast showTip:resultTip gravity:iToastGravityBottom];
}
- (void)updateState:(NSArray*)states
{
    [GKBMainController updateEqu:self.equipment button:nil states:states];
}
#pragma mark - 控制90
- (void)ctr90:(NSString *)ORDER
{
    NSString *order = ORDER;
    NSString *orderName = NSLocalizedString(order, nil);
    NSString *operat = NSLocalizedString(@"operat:", nil);
    NSString *startTip = [operat stringByAppendingString:orderName];
    [iToast showTip:startTip gravity:iToastGravityBottom];
    
    self.ctrOrder = order;
    NSString *switchId = self.equipment.switchId;
    NSString *sessionId = [LoginController1 sessionID];
    NSString *fomat = @"[{\"orderId\":\"%@\", \"switchId\":\"%@\", \"sessionId\":\"%@\"}]";
    NSString *body = [NSString stringWithFormat:fomat, order, switchId, sessionId];
    
    //操作失败！原因：
    NSString *errorTip = [OperationTip operatSuccess:NO type:ErrorTypeNetworkFail];
    [WebServices requestStartOrder:@"ctl_Intelligent_Wardrobe" body:body target:self action:@selector(ctr90End:) errorTip:errorTip];
}
- (void)ctr90End:(NSDictionary *)dic
{
    NSNumber *result = [dic objectForKey:@"result"];
    //    NSString *order = NSLocalizedString(self.ctrOrder, nil);
    if (result.integerValue == 0) {
        //操作成功
        NSString *tip = [OperationTip operatSuccess:YES type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
    else
    {
        //操作失败！原因：
        NSString *tip = [OperationTip operatSuccess:NO type:ErrorTypeServerReturnFail];
        [iToast showTip:tip gravity:iToastGravityBottom];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)up:(id)sender {
    [self ctlDevice:YES];
}
- (IBAction)down:(id)sender {
    [self ctlDevice:NO];
}
- (IBAction)stop:(id)sender {
    [self ctr90:@"Stop"];
}

- (void)dealloc {
    [_equipment release];
    [_ctrOrder release];
    [_up release];
    [_down release];
    [_stop release];
    [_imageView release];
    [super dealloc];
}
@end
